<?php
include_once("db.php");

class Class_ {
	var $template = "";
	var $attrindex = "/* attributes */";
	var $methodindex = "/* methods */";
	var $attrtemplate = "";
	var $methodtemplate = "";
	var $attrs = array();
	var $methods = array();
	var $params = array();

	var $classname;
	var $parent;

	function Class_($classname, $parent = "") {
		$this->template = file_get_contents("template.php");
		var_dump($this->template);

		$this->classname = $classname;
		$this->parent = $parent;
		$this->attrtemplate = $this->getAttrSection();
		$this->methodtemplate = $this->getMethodSection();
	}

	function getAttrSection() {
		if($this->template == "") return "";

		$index = strpos($this->template, $this->attrindex);
		$start = strpos($this->template, "\n", $index) + 1;
		$end = strpos($this->template, "\n", $start);

		$result = substr($this->template, $start, $end-$start);
		$this->template = str_replace($result, "", $this->template);
		return $result;
	}

	function getMethodSection() {
		if($this->template == "") return "";

		$index = strpos($this->template, $this->methodindex);
		$start = strpos($this->template, "\n", $index) + 1;
		$end = strpos($this->template, "}", $start) + 1;

		$result = substr($this->template, $start, $end-$start);
		$this->template = str_replace($result, "", $this->template);
		return $result;
	}

	function addMethod($methodname, $body, $param = "") {
		$this->methods[$methodname] = $body;
		$this->params[$methodname] = $param;
	}

	function addAttribute($name) {
		$this->attrs[$name] = "";
	}

	function getClass() {
		$attr_str = "";
		$method_str = "";

		foreach($this->attrs as $name => $dummy)
		{
			$attr_str .= str_replace('$var', $name, $this->attrtemplate);
			$attr_str .= "\n";
		}

		foreach($this->methods as $methodname => $body)
		{
			$temp = "";
			$temp = str_replace('$functionname', $methodname, $this->methodtemplate);
			$temp = str_replace('$params', $this->params[$methodname], $temp);
			$temp = str_replace('$body', $body, $temp);
			$method_str .= $temp."\n\n";
		}

		$result = str_replace($this->attrindex, $attr_str, $this->template);
		$result = str_replace($this->methodindex, $method_str, $result);
		$result = str_replace('$classname', $this->classname, $result);
		$result = str_replace('$parentclassname', $this->parent, $result);

		return $result;
	}
}

class MakeActiveRecode_
{
	//read a table
	//get fields
	//make method (save, find, delete, find_by_$field, get/set$field)
	var $fields;
	var $class;
	function makeActiveRecode_($tablename)
	{
		$this->class_ = new Class_($tablename, "ActiveRecode");

		$this->makeConstructor($tablename);
		$this->makeSave();
		$this->makeDelete();
		$this->makeFinds();
	}

	function makeConstructor($tablename)
	{
		$sql = "select * from `".$tablename."` limit 1";
		$result = get_rows(query($sql), MYSQL_ASSOC);
		foreach($result[0] as $key => $item)
		{
			$this->fields[] = $key;
			$this->class_->addAttribute($key);
		}
		$this->class_->addAttribute("fields");
		$this->class_->addAttribute("fields_name_collection");

		$body = ' 
		$this->'.$this->fields[0].' = $id;
		$i = 0;
		$this->tablename = "'.$tablename.'";
		$datas = array();
		if($id > 0)
		{
			$this->fields_name_collection = array();
			$datas = $this->select_("'.$this->fields[0].'", $id);';
		foreach($this->fields as $key => $item)
		{
			//$body .= "\t\t\t\t\$this->".$item." = \$datas['".$item."'];\n";
			$body .= "\t\t\t\$this->fields[".$item."] = \$datas['".$item."'];\n";
			$body .= "\t\t\t\$this->fields_name_collection[] = \"".$item."\";\n";
		}
		$body .='
		}';

		$this->class_->addMethod($tablename, $body, '$id = -1');

	}
	function makeSave()
	{
		$body = '
		//update
		$id = $this->fields[\$this->fields_name_collection[0]];
		if($id > 0)
		{
			$this->update_($this->fields, $this->fields_name_collection[0]." = ".$id);
		}
		else	//insert
		{
			$this->insert_($this->fields);
		}
		';
		$this->class_->addMethod("save", $body);
	}

	function makeFinds()
	{
		foreach($this->fields as $field_name)
		{
			$body = '
		$id = $this->fields[$this->fields_name_collection[0]];
		$datas = $this->select_("'.$field_name.'", $index);';
			foreach($this->fields as $key => $item)
				$body .= "\$this->fields['".$item."'] = \$datas['".$item."'];";

			$this->class_->addMethod("find_by_".$field_name, $body, "\$index");
		}
	}

	function makeDelete()
	{
		$body = '
		if($id > 0)
		{
			$this->delete_($this->fields_name_collection[0]." = ".$id);
		}
		else
		{
			$id = $this->fields[$this->fields_name_collection[0]];
			$this->delete_($this->fields_name_collection[0]." = ".$id);
		}
		';
		$this->class_->addMethod("destroy", $body, "\$id = -1");
	}

	function get()
	{
		return $this->class_->getClass();
	}
}

?>
